﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using MySql.Data.MySqlClient;

namespace eStock.SQL
{
    public abstract class SQLDatabase
    {
        public string LastCommand { get; set; }
        public SQLError LastError { get; set; }

        public abstract bool Connect();
        public abstract bool Connect(string server, string database, string user, string pass, int port = 3306);
        public abstract bool TryConnect(string server, string database, string user, string pass, int port = 3306);
        public abstract void Disconnect();

        public abstract SQLDataReader QueryRead(string sqlcommand);
        public abstract long QueryWrite(string sqlcommand);
        public abstract long QueryWrite(string sqlcommand, Dictionary<string, object> parameters);

        public abstract Dictionary<string, object> FetchArray(SQLDataReader dbReader);
        public abstract int FetchCount(SQLDataReader dbReader);

        public virtual string EscapeString(string input)
        {
            if (string.IsNullOrEmpty(input))
                return string.Empty;

            return Regex.Replace(input, @"[\r\n\x00\x1a\\'""]", @"\$0");
        }

        public abstract string DateFormat(string r, string f);
       

        protected virtual void DatabaseError(SQLError msg)
        {
            MessageBox.Show(msg.ErrorMessage, "Fatal Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }
}
